﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.Commons;
using YT.Controls;
using YT.DB;
using YT.Modules;

namespace YT.XH.SuddenDeafness
{
    public partial class frmTherapeuticMethodMtn : Form
    {
        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private bool mblnIsInProvince = false;
        public frmTherapeuticMethodMtn()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        public frmTherapeuticMethodMtn(string strSickRecID )
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }

        private void FillGrid()
        {
            string strSQL = string.Empty;
            ArrayList arrList = new ArrayList();

            arrList.Add(" 单纯经鼓室给地塞米松");
            arrList.Add(" 单纯经鼓室给甲基强的松龙");
            arrList.Add(" 口服激素");
            arrList.Add(" 口服激素＋经鼓室给地塞米松（同期应用）");
            arrList.Add(" 口服激素＋高压氧");
            arrList.Add(" 口服激素＋改善微循环药物");
            arrList.Add(" 口服激素＋降纤药物");
            arrList.Add(" 口服激素＋降纤药物＋改善微循环药物");
            arrList.Add(" 疗程结束后听力未达痊愈者选用方案");

            arrList.Add(" 单纯经鼓室给地塞米松");
            arrList.Add(" 单纯经鼓室给甲基强的松龙");
            
            arrList.Add(" 经鼓室给地塞米松＋高压氧");
            arrList.Add(" 经鼓室给地塞米松＋改善微循环药物");

            arrList.Add(" 方案9适应对象：①已完成第一阶段治疗，愿意接受第二阶段治疗的");
            
          
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnContraindication_Click(object sender, EventArgs e)
        {
            frmSDMethodMtn frmMethod = new frmSDMethodMtn();
            //frmMethod.MdiParent = SysVar.mfrmMdiPar;
            frmMethod.ShowDialog();
        }

        private void frmTherapeuticMethodMtn_Load(object sender, EventArgs e)
        {
            this.Text = "方案选取及说明   " + "患者编号" + SysVar.SickRecID; 
            this.Text += ",   患者姓名" +  DataFunctions.GetFieldValue ("mSickPerson", "SickName" , "  Type='SuddenDeafness' and SickRecID='"+  SysVar.SickRecID +"' ");
            FillCurOperation();
            initControl();
            SelectMethod();
            timer1.Start(); 
        }

        
        private void initControl()
        {
            string strSQL=string.Empty ;
            DBAccess dbcon =new DBAccess (false,false );
            strSQL = "select * from mHospital where IsCurHospital=1 and HospitalID='" + SysVar.HospitalID + "' and InProvince=1";
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
             

            if ( drRow !=null )
                mblnIsInProvince=true ;
            else 
                mblnIsInProvince=false ;

            rad方案10.Visible = mblnIsInProvince;
            pnl3.Visible = mblnIsInProvince;

            if (mblnIsInProvince)
            {
                lblDesc1.Text = "  1、无口服激素禁忌症者： 随机进入第1－5和方案10 方案组；";
                lblDesc2.Text = "  2、有口服激素禁忌症者： 随机进入6 - 9和方案10 方案组。";
                lblDescOther.Text = @"1.  就诊前未行口服激素治疗，随机进入方案1-5（原已用溶栓治疗者停用溶栓治疗，将会随机进入1-3组）；
2.  就诊前已行口服激素治疗者，停用口服激素治疗，随机进入方案6-10（原已用溶栓治疗者停用溶栓治疗，将会随机进入6-7组）";
            }
            else
            {
                lblDesc1.Text = "  1、无口服激素禁忌症者： 随机进入第1－5方案组；";
                lblDesc2.Text = "  2、有口服激素禁忌症者： 随机进入以下方案组。";
                lblDescOther.Text = @"1.  就诊前未行口服激素治疗，随机进入方案1-5（原已用溶栓治疗者停用溶栓治疗，将会随机进入1-3组）；
2.  就诊前已行口服激素治疗者，停用口服激素治疗，随机进入方案6-9（原已用溶栓治疗者停用溶栓治疗，将会随机进入6-7组）";
            }
            
            strSQL = "select dateDiff(d,PainDate, FirstDate),CuredInOtherHosiptal,UseRS,UseKFJS from mSickPerson where SickRecID='" + mstrSickRecID + "'";
            DataRow drDays = mdbcon.GetDataFirstRow(strSQL);
            int intDays = 0;
            bool blnCuredInOtherHis = false;   //已在外院接受过治疗
            bool blnUseRs = false;   // 治疗时已用过融栓药
            bool blnUseKFJS = false;  //在外院治疗时是否用过降纤治疗 
            if (drDays != null)
            {
                intDays =Math.Abs ( CvtFunc.ToInt(drDays[0]));
                blnCuredInOtherHis = CvtFunc.ToBoolean(drDays["CuredInOtherHosiptal"]);
                blnUseRs = CvtFunc.ToBoolean(drDays["UseRS"]);
                blnUseKFJS = CvtFunc.ToBoolean(drDays["UseKFJS"]);
            }

            if (blnCuredInOtherHis ==true ) lblInOneMonth.Text += " ,在外院接受过治疗";
            if (blnUseKFJS == true) lblAfterOneMonth.Text += " 并已使用过降纤治疗";
            
            if (blnCuredInOtherHis == true) lblAfterOneMonth.Text += " ,在外院接受过治疗";
            if (blnUseKFJS == true) lblAfterOneMonth.Text += " 并已使用过口服激素治疗";
            

            //设置第二屏的说明
            if (intDays > 30 || (blnCuredInOtherHis || blnUseKFJS || blnUseRs))
            {
                lblDesc4.Text = "1. 第一阶段选用方案6 患者可继续沿用原治疗方案（方案6→方案A.1)";
                lblDesc3.Text = "2. 第一阶段选用方案7－9患者，停用口服降纤治疗。";
            }
                        
            bool isInProvince = CvtFunc.ToBoolean(DataFunctions.GetFieldValue("mHospital", "InProvince", "HospitalID='" + SysVar.HospitalID + "'"));
            if (isInProvince == true)
            {
                lblDesc4.Text = "1. 第一阶段选用方案6 患者可继续沿用原治疗方案（方案6→方案A.1)";
                if ( !pnl1.Visible )
                    lblDesc3.Text = "2. 第一阶段选用方案7 － 10患者，停用口服降纤治疗。";
                else
                    lblDesc3.Text = "2. 第一阶段选用方案2 － 5患者，停用口服降纤治疗。";
            }

            strSQL = " select distinct contraindicationname from mTherapeuticContraindication A" +
                            " inner join mContraindication B on a.contraindication =b.contraindicationType " +
                            " where sickrecid='" + mstrSickRecID + "' and B.Type='" + TitleType.SuddenDeafness + "' and a.Type=B.Type";
            DataRow drKFJS = mdbcon.GetDataFirstRow(strSQL);
            if ( drKFJS !=null)
            {
                if (CvtFunc.ToString(drKFJS[0]) == "口服激素") blnUseKFJS = true;
            }

            if (intDays <= 30 && !(blnCuredInOtherHis || blnUseKFJS))
            {
                pnl1.Visible = true;
                pnl2.Visible = false;
            }
            else if (intDays <= 30 && blnUseRs)
            {
                pnl1.Visible = true;
                pnl2.Visible = false;
            }
            else if (intDays <= 30 && !blnUseKFJS && !blnUseRs)
            {
                pnl1.Visible = true;
                pnl2.Visible = false;
            }
            else
            {
                pnl1.Visible = false;
                pnl2.Visible = true;
            }

            rad方案1.Enabled = false;
            rad方案2.Enabled = false;
            rad方案3.Enabled = false;
            pnl_方案3.Enabled = false; 
            rad方案4.Enabled = false;
            rad方案5.Enabled = false;
            pnl_方案5.Enabled = false;
            rad方案6.Enabled = false;
            rad方案7.Enabled = false;
            pnl_方案7.Enabled = false; 
            rad方案8.Enabled = false;
            rad方案9.Enabled = false;
            pnl_方案9.Enabled = false; 
            rad方案10.Enabled = false;
            pnl_方案10.Enabled = false;

            if (mblnIsInProvince)
            {
                rad方案3C.Visible = false;
                rad方案5C.Visible = false;
                rad方案7C.Visible = false;
                rad方案9C.Visible = false;
                rad方案10C.Visible = false;
                rad方案AC.Visible = false;

                txt方案3D.Visible = false;
                txt方案5D.Visible = false;
                txt方案7D.Visible = false;
                txt方案9D.Visible = false;
                txt方案10D.Visible = false;
                txt方案AD.Visible = false;
            }
            else
            {

            }
        }

        private void SelRad方案(object sender, EventArgs e)
        {
            RadioButton radCon = sender as RadioButton;
            if (radCon == null) return;

            if (radCon.Name == this.rad方案10.Name)
            {
                if (rad方案10.Checked)
                {
                    rad方案1.Checked = false;
                    rad方案2.Checked = false;
                    rad方案3.Checked = false;
                    rad方案4.Checked = false;
                    rad方案5.Checked = false;
                    rad方案6.Checked = false;
                    rad方案7.Checked = false;
                    rad方案8.Checked = false;
                    rad方案9.Checked = false;
                }
            }
            else
            {
                if (rad方案1.Checked || rad方案2.Checked || rad方案3.Checked ||
                     rad方案4.Checked || rad方案6.Checked || rad方案6.Checked ||
                     rad方案7.Checked || rad方案8.Checked || rad方案9.Checked)
                {
                    rad方案10.Checked = false;
                    rad方案10A.Checked = false;
                    rad方案10B.Checked = false;
                }
            }
        }



        private void SelectMethod()
        {
            string strContr = string.Empty; //本患者的禁忌症
            string strFixCase = string.Empty;  //本患者适用方案
            int  intMaxRecID =0;

            string strSQL = string.Empty;
            strSQL = "select * from mSickPerson where sickrecid='"+ mstrSickRecID  +"'";
            DataRow drCheck = mdbcon.GetDataFirstRow(strSQL);
            if (drCheck == null )
            {
                return;
            }
            

            strSQL = "select dateDiff(d,PainDate, FirstDate),CuredInOtherHosiptal,UseRS,UseKFJS from mSickPerson where SickRecID='" + mstrSickRecID + "'";
            DataRow drDays = mdbcon.GetDataFirstRow( strSQL );
            int intDays = 0;
            bool blnCuredInOtherHis = false;   //已在外院接受过治疗
            bool blnUseRs = false;   // 治疗时已用过融栓药
            bool blnUseKFJS = false;  //是否在外院治疗用过口服激素？
            if ( drDays!= null ) 
            {
                intDays =Math.Abs ( CvtFunc.ToInt ( drDays[0]));
                blnCuredInOtherHis = CvtFunc.ToBoolean(drDays["CuredInOtherHosiptal"]);
                blnUseRs = CvtFunc.ToBoolean(drDays["UseRS"]); 
                blnUseKFJS = CvtFunc.ToBoolean ( drDays ["UseKFJS"]);
            }

            //设置第二屏的说明
            if (intDays > 30 || (blnCuredInOtherHis && blnUseKFJS))
            {
                lblDesc4.Text = "1. 第一阶段选用方案6 患者可继续沿用原治疗方案（方案6→方案A.1)";
                lblDesc3.Text = "2. 第一阶段选用方案7－9患者，停用口服激素治疗。";
            }

            strSQL = " select distinct contraindicationname from mTherapeuticContraindication A" +
                            " inner join mContraindication B on a.contraindication =b.contraindicationType " +
                            " where sickrecid='" + mstrSickRecID + "' and B.Type='" + TitleType.SuddenDeafness + "' and a.Type=B.Type";
            DataRow drKFJS = mdbcon.GetDataFirstRow(strSQL);
            if (drKFJS!=null )
            {
                if (CvtFunc.ToString(drKFJS[0]) == "口服激素") blnUseKFJS = true;
            }
            if (intDays <= 30 && !(blnCuredInOtherHis || blnUseKFJS ))
            {
                pnl1.Visible =true ;
                pnl2.Visible =false ; 
            }
            else if (intDays <= 30 && blnUseRs)
            {
                pnl1.Visible = true;
                pnl2.Visible = false; 
            }
            else if (intDays <= 30 && !blnUseRs && !blnUseKFJS)
            {
                pnl1.Visible = true;
                pnl2.Visible = false; 
            }
            else
            {
                pnl1.Visible = false;
                pnl2.Visible = true;
            }
            ArrayList arrName =new ArrayList ();
            strSQL = " select distinct contraindicationname from mTherapeuticContraindication A" +
                            " inner join mContraindication B on a.contraindication =b.contraindicationType " +
                            " where sickrecid='" + mstrSickRecID + "' and B.Type='" + TitleType.SuddenDeafness + "' and a.Type=B.Type";
                            
            DataTable dtTable = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtTable.Rows)
            {
                strContr += ";" + CvtFunc.ToString(drRow["contraindicationname"]);
            }
            if ( strContr.Length > 1) strContr ="此患者有: " + strContr.Substring(1, strContr.Length - 1)  + " 禁忌症 ";

            strSQL = " select recid, name from mTherapeuticMethod where name not in " +
                     " (select therapeuticmethod from mTherapeuticContraindication A " +
                     " inner join mContraindication B on a.contraindication =b.contraindicationType " +
                     " where sickrecid='"+ mstrSickRecID +"' and B.Type='SuddenDeafness'  )" +
                     " and (name<>'方案A.1' and name<>'方案A.2' ) " +
                     " and Type='SuddenDeafness'" +
                     ( mblnIsInProvince ? "": " and name <>'方案10'") ;
            if (intDays <= 30 && !(blnCuredInOtherHis  || blnUseKFJS))
            {
                 strSQL +=" and (name<>'方案6' and name<>'方案7' and name<>'方案8' and name<>'方案9') ";
            }
            else if ( intDays <=30 && blnUseRs )  //在外院治疗时已用过融栓药的，这时不再使用融栓方案,如果是30天内的就用”1，2，3“方案。
            {
                strSQL += " and (name<>'方案4' and name<>'方案5' and name<>'方案10' ) ";
                strSQL += " and (name<>'方案6' and name<>'方案7' and name<>'方案8' and name<>'方案9' )";
            }
            else if (intDays <= 30 && !blnUseKFJS && !blnUseRs)
            {
                strSQL += " and  (name<>'方案6' and name<>'方案7' and name<>'方案8' and name<>'方案9' )";
            }
            else
            {
                strSQL += " and (name<>'方案1' and name<>'方案2' and name<>'方案3' and name<>'方案4' and name<>'方案5' ) ";
            }
            if ( intDays >30 && blnUseRs)
            {
                strSQL += " and (  name<>'方案8' and name<>'方案9' and name<>'方案10') ";
            }
            
            if (blnUseKFJS == true) //是否在外院治疗用过口服激素的，这时不再使用口服激素
            {
                strSQL += " and (name<>'方案2'and name<>'方案3' and name<>'方案4' and name<>'方案5')";
            }

            DataTable dtMethod = mdbcon.GetDataTable(strSQL);
            foreach ( DataRow drRow in dtMethod .Rows )
            {
                strFixCase += " ;" + CvtFunc.ToString(drRow["Name"]); 
                arrName.Add( CvtFunc.ToInt ( drRow["RecID"]));

                #region 设置不可用的方案 的 enable = false

                SetEnableRadFromOperationName(CvtFunc.ToString(drRow["Name"]));
                #endregion
            }


            if (CvtFunc.ToString(drCheck["CurOperation"]) != "")
            {
                txtCase.Text = strContr; 
                return;
            }
            if (strFixCase.Length >= 1)
            {
                strFixCase = "此患者适用 " + strFixCase.Substring(1, strFixCase.Length - 1);
                strFixCase += "\r\n ";
                strFixCase += "本系统推荐使用";

                intMaxRecID = CvtFunc.ToInt(DataFunctions.GetFieldValue("mTherapeuticMethod", "max(RecID)", "1=1"));
                Random rad = new Random();
                int intRad = 0;
                while (!arrName.Contains(intRad))
                {
                    intRad = rad.Next(0, intMaxRecID);
                }
                strSQL = " select recid, name from mTherapeuticMethod where RecID='" + intRad + "'";
                DataRow drCase = mdbcon.GetDataFirstRow(strSQL);
                if (drCase != null)
                {
                    strFixCase += CvtFunc.ToString(drCase["Name"]).Trim();
                }
                else
                {
                    strFixCase = "建议使用 改善微循环药物 和 高压氧 方案治疗。";
                }

                string strCurCase = DataFunctions.GetFieldValue("mSickPerson", "CurOperation", "SickRecID ='" + mstrSickRecID + "'");
                if (strCurCase == "")
                {
                    txtCase.Text =( (strContr!="") ?  strContr + "\r\n" + strFixCase  :  strFixCase) ;
                    GetRadFromOperationName(CvtFunc.ToString ( drCase["Name"] ));
                }
                else
                    txtCase.Text = strContr;
                return;
            }
            txtCase.Text = strContr;
        }

        private void FillCurOperation()
        {
            if (mstrSickRecID == "") return;
            string strSQL = string.Empty;
            strSQL = "select * from mSickPerson where sickrecid='"+ mstrSickRecID  +"'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow == null || CvtFunc.ToString(drRow["CurOperation"]) == "")
            {
                if (CvtFunc.ToString(drRow["CurOperation"]) == "")
                {
                    //pnlNext.Enabled = false;
                    tbcMain.TabPages.Remove(tabSecond);
                }
                else
                {
                    pnlNext.Enabled = true;
                }
                return;
            }
            GetRadFromOperationName(CvtFunc.ToString(drRow["CurOperation"]));

            strSQL = "select * from mTreatHistory where SickRecID='" + mstrSickRecID + "' and recid in ( select max(recid) as RecID from mTreatHistory where sickRecID='" + mstrSickRecID + "' group by period)  order by  period,recid";
            DataTable dtCase = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drCur in dtCase.Rows)
            {
                string strPeriod = CvtFunc.ToString(drCur["Period"]);
                if (strPeriod == "1" || strPeriod == "2")
                {
                    GetRadFromOperationName(CvtFunc.ToString(drCur["TreatCaseName"]));
                    if (strPeriod == "1")
                    {
                        tbcMain.TabPages.Remove(tabSecond);
                    }
                    if (strPeriod == "2")
                    {
                        groupBox1.Enabled = false;
                        panel1.Enabled = false;
                        tbcMain.TabPages.Add(tabSecond);
                    }
                }
                else
                {
                    groupBox2.Enabled = false;
                    panel3.Enabled = false; 
                    AddNewTabPage ();
                }               
            }

            foreach (TabPage tabpage in tbcMain.TabPages)
            {
                if (tabpage.Name != "tabFirst" && tabpage.Name != "tabSecond")
                {
                    foreach (Control cons in tabpage.Controls)
                    {
                        if (cons.GetType().Name == "frmTherapeuticMethodDetail")
                        {
                            frmTherapeuticMethodDetail frmDetail = (frmTherapeuticMethodDetail)cons;
                            ((frmTherapeuticMethodDetail)frmDetail).UpdateData();
                        }
                    }
                }
            }

            if (tbcMain.TabPages.Count >= 2)
            {
                tbcMain.SelectedTab = tbcMain.TabPages[tbcMain.TabPages.Count-1 ]  ;
                pnlNext.Visible = false;
            }

            foreach ( DataRow drCur in dtCase.Rows )
            {
                string strCaseName = CvtFunc.ToString(drCur["TreatCaseName"]);
                if (drCur != null)
                {
                    if (rad方案3.Checked && strCaseName == "方案3")
                    {
                        if (SetThrombolysis(this.pnl_方案3, drCur))
                        {
                            txt方案3D.Text = CvtFunc.ToString(drCur["Thrombolysis"]);
                            rad方案3C.Checked = true;
                        }                                                         
                    }
                    else if (rad方案5.Checked && strCaseName == "方案5")
                    {
                        if (SetThrombolysis(this.pnl_方案5, drCur))
                        {
                            txt方案5D.Text = CvtFunc.ToString(drCur["Thrombolysis"]);
                            rad方案5C.Checked = true;
                        }                        
                    }
                    else if (rad方案7.Checked && strCaseName == "方案7")
                    {
                        if (SetThrombolysis(this.pnl_方案7, drCur))
                        {
                            txt方案7D.Text = CvtFunc.ToString(drCur["Thrombolysis"]);
                            rad方案7C.Checked = true;
                        }                    
                    }
                    else if (rad方案9.Checked && strCaseName == "方案9")
                    {
                        if (SetThrombolysis(this.pnl_方案9, drCur))
                        {
                            txt方案9D.Text = CvtFunc.ToString(drCur["Thrombolysis"]);
                            rad方案9C.Checked = true;
                        }                    
                    }
                    else if (rad方案10.Checked && strCaseName == "方案10")
                    {
                        if (SetThrombolysis(this.pnl_方案10, drCur))
                        {
                            txt方案10D.Text = CvtFunc.ToString(drCur["Thrombolysis"]);
                            rad方案10C.Checked = true;
                        }                        
                    }
                    else if (rad方案A2.Checked && strCaseName == "方案A.2")
                    {                        
                        if (SetThrombolysis(this.pnl_方案A2, drCur))
                        {
                            txt方案AD.Text = CvtFunc.ToString(drCur["Thrombolysis"]);
                            rad方案AC.Checked = true;
                        }                    
                    }
                }
            }
        }

        private bool SetThrombolysis(Panel pnl ,DataRow drCur)
        {
            foreach (Control con in pnl.Controls)
            {
                if (con.GetType().Name == "RadioButton")
                {
                    if (CvtFunc.ToString(drCur["Thrombolysis"]).Trim() == con.Text.Trim())
                    {
                        ((RadioButton)con).Checked = true;
                        return false ;
                    }
                }                
            }
            return true ;
        }

        private void GetThrombolysis(Control con, DataRow drRow)
        {
            if (con.GetType().Name == "RadioButton")
            {
                if (((RadioButton)con).Checked)
                {
                    drRow["Thrombolysis"] = con.Text;
                }                
            }    
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateData()) this.Close();
        }

        private bool ValidCheck( DataRow drRow  )
        {
            string strCaseName = CvtFunc.ToString(drRow["TreatCaseName"]);

            if ( strCaseName =="方案3" || strCaseName =="方案5" || strCaseName =="方案7"
                || strCaseName =="方案9" || strCaseName =="方案10" )
            if ( CvtFunc.ToString (  drRow["Thrombolysis"] )== "")
            {
                MessageBox.Show("必须选择 改善微循环 药物.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private bool UpdateData()
        {
            int intCaseCount = 1;
            string strSQL = string.Empty;
            string strFields = "SickRecID,TreatCaseName,Thrombolysis,Period,IsCurCase";
            strSQL = "update mTreatHistory set IsCurCase=0 where SickRecID='" + mstrSickRecID + "'";
            mdbcon.ExecuteSql(strSQL);

            intCaseCount = tbcMain.TabPages.Count;
            int i = intCaseCount;
            if (i <= 2)
            {
                strSQL = "select * from mTreatHistory where SickRecID='" + mstrSickRecID + "' and Period='" + i + "'";
                DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
                if (drRow == null)
                {
                    if (intCaseCount == 1)
                    {
                        drRow = mdbcon.GetDataTable(strSQL).NewRow();
                        drRow["SickRecID"] = mstrSickRecID;
                        drRow["TreatCaseName"] = GetOperationNameFromRad(i);
                        if (rad方案3.Checked)
                        {
                            foreach (Control con in this.pnl_方案3.Controls)
                            {                                
                                GetThrombolysis(con, drRow);                                
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案3D.Text;                            
                        }
                        else if (rad方案5.Checked)
                        {
                            foreach (Control con in this.pnl_方案5.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案5D.Text;                            
                        }
                        else if (rad方案7.Checked)
                        {
                            foreach (Control con in this.pnl_方案7.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案7D.Text;  
                        }
                        else if (rad方案9.Checked)
                        {
                            foreach (Control con in this.pnl_方案9.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案9D.Text;  
                        }
                        else if (rad方案10.Checked)
                        {
                            foreach (Control con in this.pnl_方案10.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案10D.Text;  
                        }
                    }
                    if (intCaseCount == 2)
                    {
                        drRow = mdbcon.GetDataTable(strSQL).NewRow();
                        drRow["SickRecID"] = mstrSickRecID;
                        drRow["TreatCaseName"] = GetOperationNameFromRad(i);
                        if (rad方案A2.Checked)
                        {
                            foreach (Control con in this.pnl_方案A2.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案AD.Text;  
                        }
                    }
                    drRow["Period"] = (i).ToString();
                    drRow["IsCurCase"] = 0;
                    if (!ValidCheck(drRow )) return false;
                    DataFunctions.UpdateTable(mdbcon, strFields, "mTreatHistory", drRow, FormStatus.AddNew);

                    strSQL = "update mSickPerson set CurOperation='" + GetOperationNameFromRad(i ) + "' where sickrecid='" + mstrSickRecID + "'";
                    mdbcon.ExecuteSql(strSQL);
                }
                else
                {
                    if (intCaseCount == 1)
                    {
                        drRow["TreatCaseName"] = GetOperationNameFromRad(i );
                    }
                    else if (intCaseCount == 2)
                    {
                        drRow["TreatCaseName"] = GetOperationNameFromRad(i);
                    }
                    if (intCaseCount == 1)
                    {
                        if (rad方案3.Checked)
                        {
                            foreach (Control con in this.pnl_方案3.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它")  drRow["Thrombolysis"] = txt方案3D.Text;
                        }
                        else if (rad方案5.Checked)
                        {
                            foreach (Control con in this.pnl_方案5.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案5D.Text;                            
                        }
                        else if (rad方案7.Checked)
                        {
                            foreach (Control con in this.pnl_方案7.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案7D.Text;                            
                        }
                        else if (rad方案9.Checked)
                        {
                            foreach (Control con in this.pnl_方案9.Controls)
                            {
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案9D.Text;                            
                        }
                        else if (rad方案10.Checked)
                        {
                            foreach (Control con in this.pnl_方案10.Controls)
                            {
                                //if (con.GetType().Name == "RadioButton")
                                //{
                                //    if (((RadioButton)con).Checked)
                                //    {
                                //        drRow["Thrombolysis"] = con.Text;
                                //    }
                                //}
                                GetThrombolysis(con, drRow); 
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案10D.Text;
                            
                        }
                    }
                    else if (intCaseCount == 2)
                    {
                        if (rad方案A2.Checked)
                        {
                            foreach (Control con in this.pnl_方案A2.Controls)
                            {
                                GetThrombolysis(con, drRow);
                            }
                            if (CvtFunc.ToString(drRow["Thrombolysis"]) == "其它") drRow["Thrombolysis"] = txt方案AD.Text;
                        }
                        else
                            drRow["Thrombolysis"] = "";
                    }

                    if (intCaseCount == 1 || intCaseCount == 2)
                    {
                        if (  !DataFunctions.CheckRecordExist(mdbcon, "mTreatHistory", CvtFunc.ToString ( drRow["TreatCaseName"]),  "TreatCaseName", "period='" + intCaseCount + "' and SickRecID='"+ mstrSickRecID +"' "))
                        {
                            DataFunctions.UpdateTable(mdbcon, strFields, "mTreatHistory", drRow, FormStatus.AddNew);
                        }
                    }
                    strSQL = "update mSickPerson set CurOperation='" + GetOperationNameFromRad(i) + "' where sickrecid='" + mstrSickRecID + "'";
                    mdbcon.ExecuteSql(strSQL);
                    DataFunctions.UpdateTable(mdbcon, "TreatCaseName,Thrombolysis", "mTreatHistory", drRow, FormStatus.Update );
                }
            }

            foreach (TabPage tabpage in tbcMain.TabPages)
            {
                if (tabpage.Name != "tabFirst" && tabpage.Name != "tabSecond")
                {
                    foreach ( Control cons in tabpage.Controls )
                    {
                        if (cons.GetType().Name == "frmTherapeuticMethodDetail")
                        {
                            frmTherapeuticMethodDetail frmDetail =(frmTherapeuticMethodDetail) cons;
                            ((frmTherapeuticMethodDetail)frmDetail).UpdateData(); 
                        }
                    }
                }
            }

            string strCurOperation = string.Empty;
            strSQL = "select TreatCaseName from mTreatHistory where Period in (select max(Period) from mTreatHistory where SickRecID='" + mstrSickRecID + "' and TreatCaseName<>'') and SickRecID='" + mstrSickRecID + "'";
            DataRow drCurCase = mdbcon.GetDataFirstRow(strSQL);
            if (drCurCase != null)
            {
                strCurOperation = CvtFunc.ToString(drCurCase["TreatCaseName"]);
            }
            strSQL = "update mSickPerson set CurOperation='" + strCurOperation + "'  where sickrecid='" + mstrSickRecID + "'  ";
            mdbcon.ExecuteSql(strSQL);

            return true;
        }

        private string GetOperationNameFromRad( int intPeriod )
        {
            if (intPeriod == 1)
            {
                if (rad方案1.Checked) return "方案1";
                if (rad方案2.Checked) return "方案2";
                if (rad方案3.Checked) return "方案3";
                if (rad方案4.Checked) return "方案4";
                if (rad方案5.Checked) return "方案5";
                if (rad方案6.Checked) return "方案6";
                if (rad方案7.Checked) return "方案7";
                if (rad方案8.Checked) return "方案8";
                if (rad方案9.Checked) return "方案9";
                if (rad方案10.Checked) return "方案10";
            }
            else if (intPeriod == 2)
            {
                if (rad方案A1.Checked) return "方案A.1";
                if (rad方案A2.Checked) return "方案A.2";                
            }
            
            return "";
        }

        

        private void SetEnableRadFromOperationName(string strOperationName)
        {
            if (strOperationName == "方案1") rad方案1.Enabled = true;
            if (strOperationName == "方案2") rad方案2.Enabled = true;
            if (strOperationName == "方案3")
            {
                rad方案3.Enabled = true;
                pnl_方案3.Enabled = true;
            }
            if (strOperationName == "方案4") rad方案4.Enabled = true;
            if (strOperationName == "方案5") 
            {
                rad方案5.Enabled = true;
                pnl_方案5.Enabled = true; 
            }
            if (strOperationName == "方案6") rad方案6.Enabled = true;
            if (strOperationName == "方案7")
            {
                rad方案7.Enabled = true;
                pnl_方案7.Enabled = true; 
            }
            if (strOperationName == "方案8") rad方案8.Enabled = true;
            if (strOperationName == "方案9")
            {
                rad方案9.Enabled = true;
                pnl_方案9.Enabled = true;
            }
            if (strOperationName == "方案10")
            {
                rad方案10.Enabled = true;
                pnl_方案10.Enabled = true; 
            }
            //}
            //else if (intPeriod == 2)
            //{
            if (strOperationName == "方案A.1") rad方案A1.Checked = true;
            if (strOperationName == "方案A.2") rad方案A2.Checked = true;
        }

        private void GetRadFromOperationName(string strOperationName)
        {
            //if (intPeriod == 1)
            //{
                if (strOperationName == "方案1") rad方案1.Checked = true;
                if (strOperationName == "方案2") rad方案2.Checked = true;
                if (strOperationName == "方案3") rad方案3.Checked = true;
                if (strOperationName == "方案4") rad方案4.Checked = true;
                if (strOperationName == "方案5") rad方案5.Checked = true;
                if (strOperationName == "方案6") rad方案6.Checked = true;
                if (strOperationName == "方案7") rad方案7.Checked = true;
                if (strOperationName == "方案8") rad方案8.Checked = true;
                if (strOperationName == "方案9") rad方案9.Checked = true;
                if (strOperationName == "方案10") rad方案10.Checked = true;
            //}
            //else if (intPeriod == 2)
            //{
                if (strOperationName == "方案A.1") rad方案A1.Checked = true;
                if (strOperationName == "方案A.2") rad方案A2.Checked = true;                
            //}
        }

        private void frmTherapeuticMethodMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            mdbcon.CloseCon();
            SysVar.marrFormList.Remove(this.Name);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (radContinue.Checked == true)
            {
                UpdateData(); 
                if (!tbcMain.TabPages.Contains(tabSecond))
                    tbcMain.TabPages.Add(tabSecond);
                tbcMain.SelectedTab = tabSecond;

            }
            else if (radEnd.Checked == true)
            {
                string strSQL = string.Empty;
                strSQL = "update mSickPerson set Finished=1 where SickRecID='" + mstrSickRecID + "'";
                mdbcon.ExecuteSql(strSQL);
                this.Close();
            }
        }

        private void btnSecondNext_Click(object sender, EventArgs e)
        {
            if (radSecondContinue.Checked)
            { if (tbcMain.TabCount == 2) AddNewTabPage(); }
            else
            {
                string strSQL = string.Empty;
                strSQL = "update mSickPerson set Finished=1 where SickRecID='" + mstrSickRecID + "'";
                mdbcon.ExecuteSql(strSQL);
                this.Close();
            }
        }

        private void changetext(string strPeriod)
        {
            AddNewTabPage();
        }

        private void AddNewTabPage()
        {
            int intPages= tbcMain.TabPages.Count+1;
            string strPages = "第"+  GetPeriodFormIntPeriod(intPages)  +"阶段";
            tbcMain.TabPages.Add("tab" +intPages, strPages);
            AddFormToNewPage(tbcMain.TabPages [ tbcMain.TabPages.Count-1]);
            tbcMain.SelectedTab = tbcMain.TabPages[tbcMain.TabPages.Count - 1];
        }


        private string GetPeriodFormIntPeriod(int intPages)
        {
            if (intPages == 3) return "三";
            if (intPages == 4) return "四";
            if (intPages == 5) return "五";
            if (intPages == 6) return "六";
            if (intPages == 7) return "七";
            if (intPages == 8) return "八";
            if (intPages == 9) return "九";
            return intPages.ToString();
        }

        private void AddFormToNewPage( TabPage tabpage)
        {
            frmTherapeuticMethodDetail frmDetail = new frmTherapeuticMethodDetail((tbcMain.TabPages.Count ).ToString (), mstrSickRecID);
            frmDetail.Name = "tab" + (tbcMain.TabPages.Count - 1).ToString();
            frmDetail.onchange += new frmTherapeuticMethodDetail.change(changetext);
            frmDetail.TopMost = false;
            frmDetail.TopLevel = false;
            frmDetail.Dock = DockStyle.Fill;
            frmDetail.FormBorderStyle = FormBorderStyle.None;
            tabpage.Controls.Add(frmDetail);
            frmDetail.Location = new Point(0, 0);
            frmDetail.Dock = DockStyle.Fill;
            frmDetail.Size = new Size(this.tbcMain.Width, this.tbcMain.Height);
            frmDetail.Visible = true;
            

            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void txt方案_TextChanged(object sender, EventArgs e)
        {
            Control con = sender as Control;
            if (con == null) return;
            if ( con.Text =="") return ;
            if (con.Name == this.txt方案3D.Name) rad方案3C.Checked = true;
            if (con.Name == this.txt方案5D.Name) rad方案5C.Checked = true;
            if (con.Name == this.txt方案7D.Name) rad方案7C.Checked = true;
            if (con.Name == this.txt方案9D .Name) rad方案9C.Checked = true;
            if (con.Name == this.txt方案10D.Name) rad方案10C.Checked = true;
            if (con.Name == this.txt方案AD.Name) rad方案AC.Checked = true;
        }
      
    }
}
